/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.github.zhangkaitao.shiro.chapter22.jcaptcha;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-3-22 下午3:46
 * <p>Version: 1.0
 */
public class JCaptcha {
    public static final MyManageableImageCaptchaService captchaService = new MyManageableImageCaptchaService();


    public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse) {
        if (request.getSession(false) == null) return false;

        boolean validated = false;
        try {
            String id = request.getSession().getId();
            validated = captchaService.validateResponseForID(request.getSession(), userCaptchaResponse).booleanValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return validated;
    }


    /**
     * hasCaptcha()：验证当前请求输入的验证码是否正确；但不从 CaptchaService 中删除已经生
     * 成的验证码（比如 Ajax 验证时可以使用，防止多次生成验证码）；
     * @param request
     * @param userCaptchaResponse
     * @return
     */
    public static boolean hasCaptcha(HttpServletRequest request, String userCaptchaResponse) {
        if (request.getSession(false) == null) return false;
        boolean validated = false;
        try {
            String id = request.getSession().getId();
            validated = captchaService.hasCapcha(request.getSession(), userCaptchaResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return validated;
    }



}
